Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.8 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class ToggleCustomHands : MonoBehaviour
  5. {
  6. public VRTK_ControllerEvents leftController;
  7. public VRTK_ControllerEvents rightController;
  8. public GameObject leftHandAvatar;
  9. public GameObject rightHandAvatar;
  10. protected bool state;
  11. protected virtual void OnEnable()
  12. {
  13. state = false;
  14. if (leftController != null)
  15. {
  16. leftController.ButtonTwoPressed += ToggleHands;
  17. }
  18. if (rightController != null)
  19. {
  20. rightController.ButtonTwoPressed += ToggleHands;
  21. }
  22. ToggleVisibility();
  23. }
  24. protected virtual void OnDisable()
  25. {
  26. if (leftController != null)
  27. {
  28. leftController.ButtonTwoPressed -= ToggleHands;
  29. }
  30. if (rightController != null)
  31. {
  32. rightController.ButtonTwoPressed -= ToggleHands;
  33. }
  34. }
  35. protected virtual void ToggleHands(object sender, ControllerInteractionEventArgs e)
  36. {
  37. state = !state;
  38. ToggleVisibility();
  39. }
  40. protected virtual void ToggleVisibility()
  41. {
  42. ToggleAvatarVisibility();
  43. ToggleSDKVisibility();
  44. ToggleScriptAlias();
  45. }
  46. protected virtual void ToggleAvatarVisibility()
  47. {
  48. if (leftHandAvatar != null)
  49. {
  50. leftHandAvatar.SetActive(state);
  51. }
  52. if (rightHandAvatar != null)
  53. {
  54. rightHandAvatar.SetActive(state);
  55. }
  56. }
  57. protected virtual void ToggleSDKVisibility()
  58. {
  59. VRTK_SDKSetup sdkType = VRTK_SDKManager.GetLoadedSDKSetup();
  60. if (sdkType != null)
  61. {
  62. VRTK_ControllerReference leftController = VRTK_ControllerReference.GetControllerReference(VRTK_DeviceFinder.GetControllerLeftHand(true));
  63. VRTK_ControllerReference rightController = VRTK_ControllerReference.GetControllerReference(VRTK_DeviceFinder.GetControllerRightHand(true));
  64. switch (sdkType.name)
  65. {
  66. case "SteamVR":
  67. ToggleControllerRenderer(leftController.actual, "Model");
  68. ToggleControllerRenderer(rightController.actual, "Model");
  69. break;
  70. case "Oculus":
  71. ToggleControllerRenderer(leftController.model);
  72. ToggleControllerRenderer(rightController.model);
  73. break;
  74. case "WindowsMR":
  75. ToggleControllerRenderer(leftController.model, "glTFController");
  76. ToggleControllerRenderer(rightController.model, "glTFController");
  77. break;
  78. }
  79. }
  80. }
  81. protected virtual void ToggleControllerRenderer(GameObject controller, string findPath = "")
  82. {
  83. if (controller != null)
  84. {
  85. if (findPath == "")
  86. {
  87. controller.SetActive(!state);
  88. }
  89. else
  90. {
  91. Transform childModel = controller.transform.Find(findPath);
  92. if (childModel != null)
  93. {
  94. childModel.gameObject.SetActive(!state);
  95. }
  96. }
  97. }
  98. }
  99. protected virtual void ToggleScriptAlias()
  100. {
  101. GameObject scriptLeft = VRTK_DeviceFinder.GetControllerLeftHand(false);
  102. GameObject scriptRight = VRTK_DeviceFinder.GetControllerRightHand(false);
  103. CycleScriptAlias(scriptLeft, leftHandAvatar);
  104. CycleScriptAlias(scriptRight, rightHandAvatar);
  105. }
  106. protected virtual void CycleScriptAlias(GameObject controller, GameObject avatar)
  107. {
  108. if (controller != null)
  109. {
  110. VRTK_InteractTouch touch = controller.GetComponentInChildren<VRTK_InteractTouch>();
  111. VRTK_InteractGrab grab = controller.GetComponentInChildren<VRTK_InteractGrab>();
  112. touch.enabled = false;
  113. grab.enabled = false;
  114. touch.customColliderContainer = null;
  115. grab.ForceControllerAttachPoint(null);
  116. if (avatar != null && state)
  117. {
  118. touch.customColliderContainer = avatar.transform.Find("HandColliders").gameObject;
  119. grab.ForceControllerAttachPoint(avatar.transform.Find("GrabAttachPoint").GetComponent<Rigidbody>());
  120. }
  121. touch.enabled = true;
  122. grab.enabled = true;
  123. }
  124. }
  125. }
  126. }